package com.china08.yxyapi.service.mg.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.entity.mg.homework.HomeworkKnowReport;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReport;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReportQues;
import com.china08.yxyapi.entity.mg.mistakes.TeaMistakesKnow;
import com.china08.yxyapi.repository.mg.homework.HomeworkKnowReportRepository;
import com.china08.yxyapi.repository.mg.mistakes.TeaMistakesKnowRepository;
import com.china08.yxyapi.service.mg.TeaMistakesKnowService;

@Service
public class TeaMistakesKnowServiceImpl implements TeaMistakesKnowService {

    @Autowired
    HomeworkKnowReportRepository homeworkKnowReportRepository;

    @Autowired
    TeaMistakesKnowRepository teaMistakesKnowRepository;

    @Autowired
    MongoOperations mongoOperations;

    /**
     * 教师同步根据班级所对应的知识点
     */
    @Override
    public void synchMistakesKnow() {
        List<Object> trenLst = new ArrayList<Object>();
        trenLst.add(null);
        trenLst.add(0);
        List<HomeworkKnowReport> dataLst = homeworkKnowReportRepository.findBySynchIn(trenLst);
        // 拆解数据
        Map<String, Map<Integer, List<HomeworkKnowReport>>> HomeworkTaskReportQuesList = dataLst.stream()
                .collect(Collectors.groupingBy(HomeworkKnowReport::getClassId,
                        Collectors.groupingBy(HomeworkKnowReport::getKnowId)));

        if (HomeworkTaskReportQuesList != null) {
            for (Entry<String, Map<Integer, List<HomeworkKnowReport>>> entry : HomeworkTaskReportQuesList.entrySet()) {
                for (Entry<Integer, List<HomeworkKnowReport>> classMap : entry.getValue().entrySet()) {
                    for (HomeworkKnowReport hkr : classMap.getValue()) {
                        TeaMistakesKnow tmk;
                        String id = hkr.getClassId() + hkr.getKnowId();
                        tmk = teaMistakesKnowRepository.findOne(id);

                        List<Integer> knowLst = new ArrayList<>();
                        knowLst.add(hkr.getKnowId());
                        // 获取已批阅数据
                        List<String> reportIdLst = getReportIdLst(hkr.getClassId());
                        int misCount = getMistakeCount(hkr.getClassId(), reportIdLst, knowLst);

                        int takCount = getTakeCount(hkr.getClassId(), reportIdLst, knowLst);

                        // 计算得到错误比率
                        double mistakesRate = ((double) misCount / (double) takCount) * 100;
                        if (tmk != null) {
                            tmk.setMistakesRate(mistakesRate);
                        } else {
                            tmk = new TeaMistakesKnow();
                            tmk.setId(id);
                            tmk.setClassId(hkr.getClassId());
                            tmk.setKnowId(hkr.getKnowId());
                            tmk.setKnowName(hkr.getKnowName());
                            tmk.setMistakesRate(mistakesRate);
                            tmk.setCreatedDate(new Timestamp(new Date().getTime()));
                        }
                        teaMistakesKnowRepository.save(tmk);
                    }
                }
            }
        }
    }

    /**
     * 根据班级获取作业信息
     * 
     * @param classId
     * @return
     */
    public List<String> getReportIdLst(String classId) {
        Criteria criteria = new Criteria();
        // 查询条件
        // 同步状态
        criteria.and("statusOfFinished").is(2);
        criteria.and("classId").is(classId);
        // 错题状态

        Query query = new Query();
        query.addCriteria(criteria);

        List<HomeworkTaskReport> dataList = mongoOperations.find(query, HomeworkTaskReport.class);

        List<String> reportIdLst = new ArrayList<>();
        for (HomeworkTaskReport homeworkTaskReport : dataList) {
            reportIdLst.add(homeworkTaskReport.getId());
        }
        return reportIdLst;
    }

    /**
     * 根据班级信息 已批阅作业信息 知识点信息获取错题数量
     * 
     * @param classId
     * @param reportIdLst
     * @param knowIdLst
     * @return
     */
    public int getMistakeCount(String classId, List<String> reportIdLst, List<Integer> knowIdLst) {
        Criteria criteria = new Criteria();
        // 查询条件
        criteria.and("classId").is(classId);
        // 查询为题型为选择题 且passed为0
        Criteria criteriaOrForAnd = new Criteria();
        criteriaOrForAnd.andOperator(Criteria.where("passed").is(0), new Criteria()
                .orOperator(Criteria.where("questionType").is("0"), Criteria.where("questionType").is("1")));
        criteria.andOperator(new Criteria().orOperator(criteriaOrForAnd, Criteria.where("mark").lt(5)),
                Criteria.where("knowIdLst").in(knowIdLst), Criteria.where("reportId").in(reportIdLst));

        Query query = new Query();
        query.addCriteria(criteria);
        int count = (int) mongoOperations.count(query, HomeworkTaskReportQues.class);
        return count;
    }

    /**
     * 根据班级信息 已批阅作业信息 知识点信息获取做题数量
     * 
     * @param classId
     * @param reportIdLst
     * @param knowIdLst
     * @return
     */
    public int getTakeCount(String classId, List<String> reportIdLst, List<Integer> knowIdLst) {
        Criteria criteria = new Criteria();
        // 查询条件
        criteria.and("classId").is(classId);

        criteria.andOperator(Criteria.where("knowIdLst").in(knowIdLst), Criteria.where("reportId").in(reportIdLst));

        Query query = new Query();
        query.addCriteria(criteria);
        int count = (int) mongoOperations.count(query, HomeworkTaskReportQues.class);
        return count;
    }
}
